package com.hpf.lease.common.result;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;

@Data
@Schema(description = "统一响应类")
public class Result<T> {
    @Schema(description = "业务状态码，200表示成功")
    private Integer code;
    private String message;
    private Boolean ok;
    private T data;
    private Result(){}
    //1 创建Result对象的方法
    public static <T> Result<T> build(Integer code , String message , Boolean ok , T data){
        Result<T> result = new Result<>();
        result.code = code;
        result.message = message;
        result.ok = ok;
        result.data = data;
        return result;
    }
    public static <T> Result<T> build(ResultCodeEnum codeEnum , Boolean ok , T data){
        return build(codeEnum.getCode(), codeEnum.getMessage(),  ok ,data);
    }
    //2 成功方法
    public static <T> Result<T> ok(T data){
        return build(ResultCodeEnum.SUCCESS.getCode(), ResultCodeEnum.SUCCESS.getMessage(), true , data);
    }
    public static <T> Result<T> ok(){
        return build(ResultCodeEnum.SUCCESS.getCode(), ResultCodeEnum.SUCCESS.getMessage(),true , null);
    }

    public static <T> Result<T> fail(Integer code , String message){
        return build(code,message,false , null);
    }
    public static <T> Result<T> fail(){
        return fail(ResultCodeEnum.FAIL.getCode(), ResultCodeEnum.FAIL.getMessage());
    }
    //使用枚举类对象创建Result对象
    public static <T> Result<T> fail(ResultCodeEnum codeEnum){
        return build(codeEnum.getCode(),codeEnum.getMessage(),false , null);
    }

    public static <T> Result<T> build(boolean b) {
        //根据布尔类型的值创建默认的成功或者失败对象返回
        return b?Result.ok():Result.fail();
    }

    //4 修改属性值链式调用的方法
    public Result<T> code(Integer code){
        this.code = code;
        return this;
    }
    public Result<T> message(String message){
        this.message = message;
        return this;
    }
    public Result<T> ok(Boolean ok){
        this.ok = ok;
        return this;
    }
    public Result<T> data(T data){
        this.data = data;
        return this;
    }
}
